(function(){
function formatParam(data) {
	if (!data.sqlId) return null;
	var sqlId = data.sqlId, sqlParam = data.sqlParam, param = {}, l1 = -1, l2 = -1;
	if (sqlId instanceof Array) {
		param.sqlId = JSON.stringify(sqlId);
		l1 = sqlId.length;
	} else {
		param.sqlId = sqlId;
	}
	if (sqlParam) {
		param.sqlParam = JSON.stringify(sqlParam);
		if (sqlParam instanceof Array) l2 = sqlParam.length;
	}
	if (l1!=-1 && l2!=-1 && l1!=l2) // 批量操作批量传参的情况，但参数组数量不同
		return null;
	if (data.dbName) param.dbName = data.dbName;
	return param;
}
function afterModify(param,data){
	if (param.complete) param.complete(data);
	if (data=="success") {
		if (param.success) param.success();
		if (param.showMessage) $.messager.alert("提示", param.message||"保存成功！", "info");
	} else {
		if (param.error) param.error();
		if (param.showMessage) $.messager.alert("提示", param.errorMessage||"保存失败！", "error");
	}
}
/**
 * 查询操作。传参示例：{
 * sqlId:"xx",
 * sqlParam:{a:1}, //可选
 * dbName:"db1", //可选
 * complete:function(data){console.log(data)},
 * async:true //可选，默认true
 * }
 */
function query(param){
	var p = formatParam(param);
	if (!p) return;
	if (param.async!=false) { //异步
		$.post("common/getData",p,param.complete,"json");
	} else { //同步，函数返回执行结果
		var ret;
		$.ajax({url:"common/getData",type:"post",data:p,async:false,dataType:"json",success:function(d){ret=d}});
		return ret;
	}
}
/**
 * 更新操作。传参示例：{
 * sqlId:"xx",
 * sqlParam:{a:1}, //可选
 * dbName:"db1", //可选
 * showMessage:true, //可选，默认true
 * message:"xx", //可选，默认"保存成功！"
 * errorMessage:"xx", //可选，默认"保存失败！"
 * complete:function(success){}, //可选
 * success:function(){}, //可选
 * error:function(){}, //可选
 * async:true //可选，默认true
 * }
 */
function modify(param){
	var p = formatParam(param);
	if (p == null) return;
	if (param.showMessage != false) param.showMessage = true;
	if (param.async!=false) { //异步
		$.post("common/modify",p,function(data){afterModify(param,data);});
	} else { //同步，函数返回success/error
		var ret;
		$.ajax({url:"common/modify",type:"post",data:p,async:false,success:function(data){afterModify(param,data);ret=data;}});
		return ret;
	}
}
/**
 * 加载表单。传参示例：{
 * form:"#form1",
 * sqlId:"xx",
 * sqlParam:{a:1}, //可选
 * dbName:"db1", //可选
 * complete:function(data){}, //可选
 * success:function(){}, //可选
 * error:function(){} //可选
 * }
 */
function loadForm(param){
	var p = formatParam(param);
	if (!p) return;
	$.post("common/getFormData",p,function(data){
		if (param.complete) param.complete(data);
		$(param.form).form("load",data);
		if (data.error && param.error) param.error();
		else if (param.success) param.success();
	},"json");
}
/**
 * 保存表单，表单中必须包含name为sqlId的元素，可以含有name为sqlParam、dbName的元素。传参示例：{
 * form:"#form1",
 * showMessage:true, //可选，默认true
 * message:"xx", //可选，默认"保存成功！"
 * errorMessage:"xx", //可选，默认"保存失败！"
 * onSubmit:function(){reutrn $(this).form('validate');}, //可选，如返回false，则终止提交
 * complete:function(success){}, //可选
 * success:function(){}, //可选
 * error:function(){} //可选
 * }
 */
function saveForm(param){
	var form = $(param.form);
	if (!form.find("[name='sqlId']").length) return; //没有sqlId
	if (param.showMessage != false) param.showMessage = true;
	var opt = {url:"common/saveForm",success:function(data){afterModify(param,data);}};
	if (param.onSubmit) opt.onSubmit = param.onSubmit;
	form.form("submit",opt);
}
/**
 * 获取数据库序列号。传参示例：{
 * name:"user_id", //序列名，string或Array
 * dbName:"db1", //数据库名，可选
 * complete:function(seqId){},
 * async:true //可选，默认true
 * }
 */
function getDBSequence(param){return getSeq(param,"getDBSequence");}
/**
 * 获取序列号。传参示例：{
 * name:"user_id", //序列名，string或Array
 * dbName:"db1", //数据库名，可选
 * complete:function(seqId){},
 * async:true //可选，默认true
 * }
 */
function getSequence(param){return getSeq(param,"getSequence");}

function getSeq(param,func){
	var p = {name:param.name instanceof Array?JSON.stringify(param.name):param.name,dbName:param.dbName||""};
	if (param.async!=false) { //异步
		$.post("common/"+func,p,param.complete,"json");
	} else { //同步，函数返回序列号
		var ret;
		$.ajax({url:"common/"+func,type:"post",data:p,dataType:"json",async:false,success:function(d){ret=d}});
		return ret;
	}
}
//服务注册
window.DBService = window.dbservice = {
	query:query,
	modify:modify,
	loadForm:loadForm,
	saveForm:saveForm,
	getSequence:getSequence,
	getDBSequence:getDBSequence
};
})();
